package com.tingyuge.base.thread.wang.complatefuture;

import com.sun.org.apache.regexp.internal.RE;

import java.time.temporal.ValueRange;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author xiaodao
 * @version 1.0
 * @date 2021/3/14
 */
public class MyTest2 {
    public static void main(String[] args) throws InterruptedException {
     /*   String result = CompletableFuture.supplyAsync(() -> "hello").thenApplyAsync(value -> value + " world").join();
        System.out.println(result);
        System.out.println("=========");

        CompletableFuture.supplyAsync(() -> "hello ").thenAccept(value -> System.out.println("welcome " + value));
        System.out.println("=========");
        //对complateState的合并
        String result2 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "hello";
        }).thenCombine(CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
                System.out.println(Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return " world";
        }), (v1, v2) -> v1 + "" + v2).join();

        System.out.println(result2);
        System.out.println("-===================");*/
        CompletableFuture<Void> completableFuture  = CompletableFuture.runAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("task finish!");
        });
        completableFuture.whenComplete((t,action)-> System.out.println("执行完成！ "));
        System.out.println("主线程执行完毕 ");
        TimeUnit.SECONDS.sleep(7);
    }
}
